﻿/**
 * 矩阵控制模块(ES6)
 */
class MatrixControl {
    constructor(progressEle, portControl) {
        this.progressEle = progressEle;
        this.portControl = portControl;
    }

    /**
     * 锁定/解锁矩阵端口
     * @param {any} matrixId
     * @param {any} doLock -> true:锁定，false:解锁
     */
    patchLock(matrixId, doLock) {
        //获取选中的出端口
        var selectingPorts = this.portControl.getSelectedDestPorts();
        if (selectingPorts.length == 0) {
            return;
        }

        //显示处理进度框
        this.progressEle.show();

        for (var i = 0; i < selectingPorts.length; i++) {
            this.portLockOrUnlock(matrixId, $(selectingPorts.get(i)), doLock);
        }

        this.progressEle.hide(selectingPorts.length);
    }

    /**
     * 锁定/解锁矩阵端口
     * @param {any} matrixId
     * @param {any} destPortElement
     * @param {any} doLock
     */
    portLockOrUnlock(matrixId, destPortElement, doLock) {
        var destPortId = destPortElement.attr('id');
        var destPort = destPortId.substr(4);
        var action = doLock ? '锁定' : '解锁';
        var portName = '端口[' + destPortElement.text() + ']';
        var self = this;

        $.ajax({
            url: doLock ? 'Lock' : 'UnLock',
            method: 'post',
            data: { 'destPort': destPort, 'matrixID': matrixId },
            datatype: "json",
            beforeSend: function (jqXHR, data) {
                self.progressEle.appendInfo(destPortId, '正在' + action + portName);
            },
            complete: function (jqXHR, textStatus) {
                var result = (textStatus == 'success' && jqXHR.responseJSON) ? '成功' : '失败';
                var resultMsg = action + portName + result;
                self.progressEle.updateInfo(destPortId, resultMsg, result);
            }
        }).success(function (result) {
            if (result === true) {
                if (doLock) {
                    destPortElement.addClass('matrixPort-locking').removeClass('matrixPort-locking-error');
                } else {
                    destPortElement.removeClass('matrixPort-locking').removeClass('matrixPort-locking-error').addClass('matrixPort');
                }
                //锁定按钮状态设定
                setLockButtonStyle();
            } else {
                self.portControl.setPortErrorStyle(destPortElement);
            }
        }).fail(function (jqXHR, textStatus) {
            self.portControl.setPortErrorStyle(destPortElement);
        });
    }

    /**
     * 矩阵端口批量切换
     * @param {any} matrixId
     */
    patchTake(matrixId) {
        //选中的源端口
        var selectedSrcPort = this.portControl.getSelectedSrcPorts();
        if (selectedSrcPort.length == 0) {
            return;
        }

        //显示处理进度框
        this.progressEle.show();

        //获取选中的源端口
        var srcPort = selectedSrcPort && selectedSrcPort.attr('id').substr(4);
        //获取选中的出端口
        var selectedDestPorts = this.portControl.getSelectedDestPorts();

        for (var i = 0; i < selectedDestPorts.length; i++) {
            var destPortElement = $(selectedDestPorts.get(i));
            this.take(matrixId, destPortElement, selectedSrcPort, srcPort);
        }

        this.progressEle.hide(selectedDestPorts.length);
    }

    /**
     * 矩阵端口切换
     * @param {any} matrixId
     */
    take(matrixId, destPortElement, srcPortElement, srcPort) {
        var destPortId = destPortElement.attr('id');
        var destPort = destPortId.substr(4);
        var destPortName = '目的端口[' + destPortElement.text() + ']';
        var srcPortName = '源端口[' + srcPortElement.text() + ']';
        var action = '切换矩阵 ' + srcPortName + ' 至 ' + destPortName;
        var self = this;

        setTimeout(function () {
            $.ajax({
                url: 'Take',
                method: 'post',
                data: { 'matrixID': matrixId, 'destPort': destPort, 'srcPort': srcPort },
                datatype: "json",
                beforeSend: function (jqXHR, data) {
                    self.progressEle.appendInfo(destPortId, '正在 ' + action);
                },
                complete: function (jqXHR, textStatus) {
                    var result = (textStatus == 'success' && jqXHR.responseJSON) ? '成功' : '失败';
                    var resultMsg = action + result;
                    self.progressEle.updateInfo(destPortId, resultMsg, result);
                }
            }).success(function (result) {
                if (result === true) {
                    //取消前一次端口操作失败样式
                    self.portControl.removePortErrorStyle(destPortElement);
                    //将切换后的新源端口更新至目的端口属性中
                    self.portControl.setLinkToSrcPort(destPortElement, srcPort);
                    //取消旧的连接源端口样式
                    if (!self.portControl.portEquals(self.portControl.linkToSrcPort, srcPortElement)) {
                        self.portControl.cancelLinkToSrcPortSelectStatus();
                        srcPortElement.addClass('matrixPort-linkToSrc-selecting').removeClass('matrixPort-selecting');
                        self.portControl.linkToSrcPort = srcPortElement;
                        //设定切换按钮样式
                        setTakeButtonStyle()
                    }
                } else {
                    self.portControl.setPortErrorStyle(destPortElement);
                }
            }).fail(function (jqXHR, textStatus) {
                self.portControl.setPortErrorStyle(destPortElement);
            });
        }, 300);
    }
}